var totalSlides = $(".container > section").size();
var wHeigth = $(window).height();
var up = 0;
var curr = 1;
var low = 2;
var lowH = 0;

function resetView(){
    wHeigth = $(window).height();
    $('.container').height(wHeigth*totalSlides);
    $('.slide').height(0);
    $('.slide').css('display', 'none');
    up = 0;
    curr = 1;
    low = 2;
    enterSlide();
}

// SLIDE CHANGE

function enterSlide(){
    $('[data-s="'+curr+'"]').css('display', 'block');
    $('[data-s="'+curr+'"]').css('margin-top', 0);
    $('[data-s="'+curr+'"]').height(wHeigth);
    $('[data-s="'+curr+'"]').width($(window).width());
    $('[data-s="'+low+'"]').css('display', 'block');
    $('[data-s="'+low+'"]').height(lowH);
    $('[data-s="'+low+'"]').width($(window).width());

    console.log(($('[data-s="'+curr+'"] .centerJs').height()/2));
    var marginTopCurrent = ($('[data-s="'+curr+'"] .centerJs').height()/2);
    var marginLeftCurrent = ($('[data-s="'+curr+'"] .centerJs').width()/2);
    $('[data-s="'+curr+'"] .centerJs').css('margin-top', (wHeigth/2)-marginTopCurrent);
    $('[data-s="'+curr+'"] .centerJs').css('margin-left', -marginLeftCurrent);

    var marginTopLower = ($('[data-s="'+low+'"] .centerJs').height()/2);
    var marginLeftLower = ($('[data-s="'+low+'"] .centerJs').width()/2);
    $('[data-s="'+low+'"] .centerJs').css('bottom', (wHeigth/2)-marginTopLower);
    $('[data-s="'+low+'"] .centerJs').css('margin-left', -marginLeftLower);
}

// ON SCROLL

$(document).scroll(function(){
    var scrollAmount = $(window).scrollTop();
    $('[data-s="'+low+'"]').css('margin-top', wHeigth-(scrollAmount-(wHeigth*up)));
    $('[data-s="'+low+'"]').height(scrollAmount-(wHeigth*up));
    if($('[data-s="'+low+'"]').height(scrollAmount-(wHeigth*up)) > wHeigth){
        $('[data-s="'+low+'"]').height(wHeigth);
    }
    
    var marginTopLower = ($('[data-s="'+low+'"] .centerJs').height()/2);
    $('[data-s="'+low+'"] .centerJs').css('bottom', ((wHeigth/2)+marginTopLower)-(((scrollAmount-(wHeigth*up))/wHeigth)*2*marginTopLower));
    
    if(scrollAmount > wHeigth*curr){
        ++up;
        ++curr;
        ++low;
        lowH = 0;
        enterSlide();
    }
    if(scrollAmount < wHeigth*up){
    $('[data-s="'+curr+'"] .centerJs').css('top', '');
    $('[data-s="'+low+'"]').css('display', 'none');
        --up;
        --curr;
        --low;
        lowH = $(window).height();
        enterSlide();
    }
});

// WAIT FOR IMGS TO LOAD

$(window).load(function() {
    $('.container').height(wHeigth*totalSlides);
    $('.container').children().each(function (i) {
        $(this).attr('data-s', i+1);
        $(this).css('z-index', i+10);
    });
    $('.uno').height(wHeigth);
    if($(window).width() > 996){
        $('.uno .centerJs').css('margin-top', (wHeigth/2)-400);
        $('.uno .centerJs').css('margin-left', '-498px');
        $('.dos .centerJs').css('margin-top', (wHeigth/2)-400);
        $('.dos .centerJs').css('margin-left', '-498px');
    }else{
        $('.uno .centerJs').css('margin-top', (wHeigth/2)-(0.8*$(window).width()/2));
        $('.uno .centerJs').css('margin-left', -$(window).width()/2);
        $('.dos .centerJs').css('margin-top', (wHeigth/2)-(0.8*$(window).width()/2));
        $('.dos .centerJs').css('margin-left', -$(window).width()/2);
    }
    $('.loader').fadeOut();
    $('.container').delay(400).fadeIn();
});

/*FUNCION CUANDO SE HACE RESIZE*/

$(window).bind('resize', function() {
	$(document).scrollTop(0);
	resetView();
});

// SCROLL TO

$('.slide:not(.notClickeable)').on('click', function(e){
   var slide = $(this).attr('data_slide');
   $('.container').scrollTop(wHeigth*slide);
});
